//
//  ActivityListViewController.m
//  UI_豆瓣项目
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 关业达. All rights reserved.
//

#import "ActivityListViewController.h"
#import "ActivityTableViewCell.h"
#import "AppTools.h"
#import "MBProgressHUD.h"
#import "ActivityDetailViewController.h"
#import "Activity.h"
#import "Activety2.h"
#import "MJRefresh.h"
@interface ActivityListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,retain)UITableView *myTableView;

@property (nonatomic, retain)NSMutableArray *myArray;

@property (nonatomic, retain)MBProgressHUD *hub;

@property (nonatomic, assign)BOOL isUPLoad;


@end

@implementation ActivityListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.848 blue:0.480 alpha:1.000];
    self.navigationItem.title = @"活动";
    
    self.myArray = [NSMutableArray array];
    
    
     //缓存部分
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
     //生成路径
    NSString *activityFilePath = [document stringByAppendingPathComponent:@"Activity.plist"];
    
    NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
    dic2 = [NSMutableDictionary dictionaryWithContentsOfFile:activityFilePath];
    if (dic2 != nil) {
        NSMutableArray *array = [dic2 objectForKey:@"events"];
        for (NSDictionary *dic in array) {
            Activity *activety = [[Activity alloc] init];
            [activety setValuesForKeysWithDictionary:dic];
            [self.myArray addObject:activety];
            [activety release];
        }
    }
    
    
    [AppTools geturlStr:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php" Block:^(id result) {
        
        
        
        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
        
        dic1 = result;
        NSLog(@"****==%@",activityFilePath);
        BOOL result33 = [dic1 writeToFile:activityFilePath atomically:YES];
        if (result33) {
            NSLog(@"****");
        }

        // 存入
            NSMutableArray *array = [dic1 objectForKey:@"events"];
       
            for (NSDictionary *dic in array) {
                Activity *activity = [[Activity alloc] init];
                //KVC
                [activity setValuesForKeysWithDictionary:dic];
                [self.myArray addObject:activity];
                [activity release];
       
                // 刷新
                [self.myTableView reloadData];
                // 停止菊花
                [self.hub hide:YES];
                
            }
    
        
    }];
    
    
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [_myTableView release];
    
    [self.myTableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    // 上下拉刷新
    [self addHeaderRefresh];
    //[self addFooterRefresh];
    // 默认不是上拉
//    self.isUPLoad = NO;

    // 菊花
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"别TM着急!";
    
}

// 设置下拉刷新
- (void)addHeaderRefresh
{
    [self.myTableView addHeaderWithCallback:^{
        NSLog(@"下拉刷新");
        
        // 缓存部分
        NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        // 生成路径
        NSString *activityFilePath = [document stringByAppendingPathComponent:@"Activity.plist"];
        
        [AppTools geturlStr:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php" Block:^(id result) {
            
            // 删除之前数组内
            [self.myArray removeAllObjects];
            
            NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
            dic1 = result;
            
           [dic1 writeToFile:activityFilePath atomically:YES];
            
            // 存入
            NSMutableArray *array = [dic1 objectForKey:@"events"];
            
            for (NSDictionary *dic in array) {
                Activity *activity = [[Activity alloc] init];
                //KVC
                [activity setValuesForKeysWithDictionary:dic];
                [self.myArray addObject:activity];
                [activity release];
               
                // 停止菊花
                [self.hub hide:YES];
            }
          
        }];
        [self.myTableView headerEndRefreshing];// 注意不要写进for循环内,否则/////
        [self.myTableView footerEndRefreshing];
        // 刷新
        [self.myTableView reloadData];
    }];
    // 进入应用自动刷新
    [self.myTableView headerBeginRefreshing];
}














- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    
    ActivityTableViewCell *cell = [[ActivityTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    Activity *activity = [self.myArray objectAtIndex:indexPath.row];
    cell.aActivity = activity;
    return cell;
    
}

// 点击cell触发方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityDetailViewController *activityDetaVC = [[ActivityDetailViewController alloc] init];
    
    Activity *activity = [self.myArray objectAtIndex:indexPath.row];
    //传整个activity
    activityDetaVC.activiyX = activity;
    [self.navigationController pushViewController:activityDetaVC animated:YES];
    
    [activityDetaVC release];
}





- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 186;
}














- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
